﻿
using ParkSaleForAdmin.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CustomComponents.CustomInputBox;
namespace ParkSaleForAdmin
{

    public class CompanyInfo
    {
        public int ID
        {
            get;
            set;
        }
        public string Name
        {
            get;set;
        }
        public string About
        {
            get;
            set;
        }
        public string Phone
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public string Type
        {
            get;
            set;
        }
        public CompanyInfo()
        {
            
        }
        public  CompanyInfo(DatabaseData data)
        {
            ID = int.Parse(data["id"].Value);
            Name = data["name"].Value;
            About = data["about"].Value;
            Phone = data["phone"].Value;
            Email = data["email"].Value;
            Type = data["type"].Value;
        }
    }

    /// <summary>
    /// CompanyManagement.xaml 的交互逻辑
    /// </summary>
    public partial class CompanyManagement : Window
    {
        int page = 0;
        int per = 5;
     public   ObservableCollection<CompanyInfo> CollectionModelFile = new ObservableCollection<CompanyInfo>();
        string[] values = new string[] { "name", "about", "phone", "email", "type" };


        string[] titles = new string[] { "企业名", "企业介绍", "联系电话", "电子邮件", "企业类型 1开发商 2承包商 0其他" };
        public CompanyManagement()
        {
            InitializeComponent();

            for (int i = 0; i < titles.Length; i++)
            {
                inputbox.Items.Add(new InputPanel() { Name = titles[i],Tag=values[i] });
            }
            Init(0);
          
        }
        void Init(int p)
        {
            page = p;
            SystemManager.Request<List<DatabaseData>>(SuUserRequestInfo.s_FindValue, new string[] { "Company", $"Limit {page * per},{per}" }, SystemManager.Admin, delegate (Object objs) {
                if (objs != null)
                {
                    var datas = (List<DatabaseData>)objs;
                    CollectionModelFile.Clear();
                    foreach (var d in datas)
                    {
                        CollectionModelFile.Add(new CompanyInfo(d));
                    }
                    listView.ItemsSource = CollectionModelFile;
                    notice.Content = $"页数：{page}";
                    
                }
            });
        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                var detial = (CompanyInfo)listView.SelectedItem;
                SystemManager.Request<int>(SuUserRequestInfo.s_DeleteValue, new string[] { "Company", $"select * from Company where id = {detial.ID}" }, SystemManager.Admin, delegate (object obj)
                {
                    if (obj != null)
                    {
                        var r = (int)obj;
                        if (r == SuUserResponseInfo<int>.OK)
                        {
                            MessageBox.Show("删除成功");
                            Init(page);
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }

                }
                );
            }
        }

        private void change_Click(object sender, RoutedEventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                var detial = (CompanyInfo)listView.SelectedItem;
                SingleOptionInputBox box = new SingleOptionInputBox(titles);
                var textinput = new InputBox();
                int select = box.Show("请选择要修改的项", "修改");
                if (select >= 0)
                {
                    string value =textinput.Show("请输入要修改的值", "修改");
                    if (value.Length > 0)
                        SystemManager.Request<int>(SuUserRequestInfo.s_ChangeValue, new string[] { "Company", $"select * from Company where id = {detial.ID}", values[select], value }, SystemManager.Admin, delegate (Object obj)
                        {
                            if (obj != null)
                            {
                                var r = (int)obj;
                                if (r == SuUserResponseInfo<int>.OK)
                                {
                                    MessageBox.Show("修改成功");
                                    Init(page);
                                }
                                else
                                {
                                    MessageBox.Show("修改失败");
                                }
                            }
                            else
                            {
                                MessageBox.Show("修改失败");
                            }


                        });
                    else
                    {
                        MessageBox.Show("空输入错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                box.Close();

            }
            else
            {
                MessageBox.Show("请点击一项进行选择", "提示");
            }
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            List<string> args = new List<string>(new string[] { "Company", "select * from Company where id=99999" });
            foreach (var item in inputbox.Items)
            {
                var panel = (InputPanel)item;
                string n = panel.Tag.ToString();

                if (panel.Value == null)
                {
                    MessageBox.Show("空输入错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    break;
                }
                else
                {
                    if (panel.Value.Length == 0)
                    {
                        MessageBox.Show("空输入错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        break;
                    }

                    if (n.Equals("type"))
                    {
                        try
                        {
                            int t = int.Parse(panel.Value);
                            if (t >= 0 && t <= 2)
                            {
                                args.Add(n);
                                args.Add(panel.Value);
                                continue;
                            }
                            else
                            {
                                MessageBox.Show("输入范围错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                        catch
                        {
                            MessageBox.Show("类型输入错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    else
                    {
                        args.Add(n);
                        args.Add(panel.Value);
                    }
                }
            }
            if (args.Count == 12)
            {
                SystemManager.Request<int>(SuUserRequestInfo.s_AppendValue, args.ToArray(), SystemManager.Admin, delegate (Object result)
                {
                    if (result != null)
                    {
                        if ((int)result == SuUserResponseInfo<Object>.OK)
                        {
                            Init(0);
                            MessageBox.Show("录入成功！");

                        }
                        else
                        {
                            MessageBox.Show($"ERROR:{result}", "录入失败！");
                        }
                    }
                    else
                    {
                        MessageBox.Show($"网络错误", "录入失败！");
                    }

                });
            }
               

        }



        private void pagechange_Click(object sender, RoutedEventArgs e)
        {
            SystemManager.Request<List<DatabaseData>>(SuUserRequestInfo.s_FindValue, new string[] { "Company", $"Limit {(page+1) * per},{per}" }, SystemManager.Admin, delegate (Object objs) {
                if (objs != null)
                {

                    var datas = (List<DatabaseData>)objs;
                    if (datas.Count > 0)
                    {
                        CollectionModelFile.Clear();
                        foreach (var d in datas)
                        {
                            CollectionModelFile.Add(new CompanyInfo(d));
                        }
                        listView.ItemsSource = CollectionModelFile;
                        notice.Content = $"页数：{++page}";
                    }
                    else
                        MessageBox.Show("无更多数据", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);

                    pagechange_next.IsEnabled = true;
                }
            },delegate() { pagechange_next.IsEnabled = false; });
        }

        private void pagechange_last_Click(object sender, RoutedEventArgs e)
        {
            if (page - 1 >= 0)
            {
                page--;
                SystemManager.Request<List<DatabaseData>>(SuUserRequestInfo.s_FindValue, new string[] { "Company", $"Limit {page * per},{per}" }, SystemManager.Admin, delegate (Object objs)
                {
                    if (objs != null)
                    {

                        var datas = (List<DatabaseData>)objs;
                        if (datas.Count > 0)
                        {
                            CollectionModelFile.Clear();
                            foreach (var d in datas)
                            {
                                CollectionModelFile.Add(new CompanyInfo(d));
                            }
                            listView.ItemsSource = CollectionModelFile;
                            notice.Content = $"页数：{page}";
                        }
                        else
                            MessageBox.Show("无更多数据", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                        pagechange_last.IsEnabled = true;
                    }
                },delegate() { pagechange_last.IsEnabled = false; });
            }
            else
            {
                MessageBox.Show("已到第一页", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

        }
    }
}
